package account

import (
	"errors"
	"orm_gin/model"
	"orm_gin/orm"
	"orm_gin/utools/res"

	"github.com/duke-git/lancet/random"
	"github.com/gin-gonic/gin"
)

// @BasePath /account

// @Summary 注册
// @Description token
// @Tags 账号
// @Param Account query string true "Account"
// @Param Password query string true "Password"
// @Param NikeName  query string false "NikeName "
// @Param Email query string false "Email"
// @Param Gender  query string false "Gender"
// @Param Phone  query string false "Phone"
// @Success 200 {object} docsmodel.ResRegnin
// @Router /account/regnin [post]
// @Security
func Regnin(c *gin.Context) {
	var regninInfo model.UserInfo

	if has := orm.DB.Migrator().HasTable(&model.UserInfo{}); !has {
		orm.DB.AutoMigrate(&model.UserInfo{})
	}

	if err := c.ShouldBind(&regninInfo); err != nil {
		res.Error(c, 0, err, "JSON 参数解析失败")
		return
	}

	var user model.UserInfo
	orm.DB.Where("account", regninInfo.Account).First(&user)

	if user.Account != "" {
		res.Error(c, 0, errors.New("重复账号"), "重复账号")
		return
	}

	// 创建uuid
	uuid, _ := random.UUIdV4()
	regninInfo.Id = uuid

	r := orm.DB.Create(regninInfo)
	if r.Error != nil {
		res.Error(c, 0, r.Error, "数据库插入错误")
		return
	}

	res.Ok(c, regninInfo, "注册成功")
}
